package com.xiaoba.product.service.impl;

import com.xiaoba.product.config.ThreadPoolExcuterConfig;
import com.xiaoba.product.domain.PmsSkuImages;
import com.xiaoba.product.domain.PmsSkuInfo;
import com.xiaoba.product.domain.PmsSpuInfoDesc;
import com.xiaoba.product.service.*;
import com.xiaoba.product.vo.ItemGroupBaseAttrVo;
import com.xiaoba.product.vo.ItemInfo;
import com.xiaoba.product.vo.ItemSaleAttr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @Author: Gd
 * @package: com.xiaoba.product.service.impl
 * @Project: XiaoBa
 * @Name: ItemServiceImpl
 * @Date: 2024/6/4 20:53
 */
@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private IPmsSkuInfoService skuInfoService;

    @Autowired
    private IPmsSkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    private IPmsSkuImagesService skuImagesService;

    @Autowired
    private IPmsSpuInfoDescService spuInfoDescService;

    @Autowired
    private IPmsProductAttrValueService productAttrValueService;

    @Autowired
    private ThreadPoolExcuterConfig threadPoolExcuterConfig;


    @Override
    public ItemInfo getItemBySkuId(Long skuId) throws ExecutionException, InterruptedException {

        ItemInfo itemInfo = new ItemInfo();


        CompletableFuture<PmsSkuInfo> pmsSkuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            // 1、获取skuInfo
            PmsSkuInfo pmsSkuInfo = skuInfoService.selectPmsSkuInfoBySkuId(skuId);
            itemInfo.setItemSkuInfo(pmsSkuInfo);

            return pmsSkuInfo;
        });

        CompletableFuture<Void> itemSaleAttrCompletableFuture = pmsSkuInfoCompletableFuture.thenAcceptAsync((res) -> {
            // 2、 获取销售属性信息
            List<ItemSaleAttr> itemSaleAttrs = skuSaleAttrValueService.getItemSaleAttrBySpuId(res.getSpuId());
            itemInfo.setItemSaleAttrs(itemSaleAttrs);
        });


        CompletableFuture<Void> skuImagesCompletableFuture = CompletableFuture.runAsync(() -> {
            // 3、 获取sku图片信息
            List<PmsSkuImages> skuImagesBySkuId = skuImagesService.getSkuImagesBySkuId(skuId);
            itemInfo.setItemSkuImages(skuImagesBySkuId);
        });


        CompletableFuture<Void> spuInfoDescCompletableFuture = pmsSkuInfoCompletableFuture.thenAcceptAsync((res) -> {
            // 4、获取spu描述信息
            PmsSpuInfoDesc pmsSpuInfoDesc = spuInfoDescService.selectPmsSpuInfoDescBySpuId(res.getSpuId());
            itemInfo.setItemSpuInfoDesc(pmsSpuInfoDesc);
        });


        CompletableFuture<Void> itemGroupBaseAttrCompletableFuture = pmsSkuInfoCompletableFuture.thenAcceptAsync((res) -> {
            //5、获取spu基本属性信息
            List<ItemGroupBaseAttrVo> itemGroupBaseAttrByCatagoryId = productAttrValueService.getItemGroupBaseAttrByCatagoryId(res.getCatalogId());
            itemInfo.setItemGroupBaseAttrVos(itemGroupBaseAttrByCatagoryId);
        });


        CompletableFuture.allOf(
                itemSaleAttrCompletableFuture,
                skuImagesCompletableFuture,
                spuInfoDescCompletableFuture,
                itemGroupBaseAttrCompletableFuture).get();// 阻塞方法

        return itemInfo;
    }
}
